(function () {
	//---------MAIN---------
	app.service('VmWareData', [function () {
		var service = {
			platformId: null,
			type: 'datacenter',
			host:{},
			datacenter:{},
			cluster:{},
			vm:{},
			template:{},
			snapshot:{},
			datastore:{},
			network:{},
			platformData:{}
		};
		return service;
	}])
	app.controller('vmTabCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', 'webSocket', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, webSocket, VmWareData) {
			$rootScope.moduleTitle = '平台管理 > VCenter';//定义当前页
			$rootScope.link = '/statics/css/user.css';
			switch (VmWareData.type) {
				case 'datacenter':
					$scope.active11 = true;
					break;
				case 'cluster':
					$scope.active12 = true;
					break;
				case 'host':
					$scope.active13 = true;
					break;
				case 'vm':
					$scope.active1 = true;
					break;
				case 'template':
					$scope.active10 = true;
					break;
				case 'snapshot':
					$scope.active4 = true;
					break;
				case 'datastore':
					$scope.active14 = true;
					break;
				case 'network':
					$scope.active6 = true;
					break;
			};
			var myScroll;
			//获取云平台列表
			var getList = function () {
				httpLoad.loadData({
					url: '/cloudVendor/list',
					method: 'POST',
					data: {
						page: 1,
						rows: 10000,
						params: JSON.stringify([{"param": {"type": "VMWARE"}, "sign": "EQ"}]),
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.platformData = data.data.rows;
							if (!data.data.total) {
								VmWareData.platformId = 0;
								loadTabData(1);
								$scope.pop('请添加平台', 'info');
								return;
							};
							if (VmWareData.platformId) {
								$scope.platformData.forEach(function (item) {
									if (item.id == VmWareData.platformId) {
										$scope.selectCell(item, 1);//有平台id直接通过select获取
									}});
							} else $scope.selectCell($scope.platformData[0]);//无平台id通过点击
							$('.platform-container>div').width(124 * (data.data.total + 1));
							myScroll = new IScroll('.platform-container', {
								scrollX: true,
								scrollY: true,
								momentum: false,
								snap: true
							});
						}
					}
				});
			};
			getList();
			$scope.goLeft = function () {
				if(myScroll) myScroll.goToPage(-10, 0);
			};
			$scope.goRight = function () {
				if(myScroll) myScroll.goToPage(10, 0);
			};
			$scope.editPlatform = function ($event, id) {
				$event.stopPropagation();
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/platform/addModal.html',
					controller: 'addVmPlatformModalCtrl',
					backdrop: 'static',
					resolve: {
						id: function () {
							return id || '';
						}
					}
				});
				modalInstance.result.then(function (data) {
					if(!id) VmWareData.platformId = data.data;//添加时默认选中添加的平台
					getList();
				});
			};
			$scope.delPlatform = function ($event, id) {
				$event.stopPropagation();
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/template/delModal.html',
					controller: 'delModalCtrl',
					backdrop: 'static',
					resolve: {
						tip: function () {
							return '此操作会删除整个平台下的所有资源，不会对原有平台上的资源做操作，确定是否删除？';
						},
						btnList: function () {
							return [{name: '删除', type: 'btn-info'}, {name: '取消', type: 'btn-cancel'}];
						}
					}
				});
				modalInstance.result.then(function () {
					httpLoad.loadData({
						url: '/cloudVendor/remove',
						method: 'POST',
						data: {id: id},
						success: function (data) {
							if (data.success) {
								getList();
								if (id == VmWareData.platformId) VmWareData.platformId = '';
								$scope.pop('平台删除成功');
							}
						}
					});
				});
			};
			$scope.selectCell = function (row,flag) {
				if (row.selected) return;
				$scope.platformData.forEach(function (item) {
					item.selected = false;
				});
				row.selected = true;
				VmWareData.platformId = row.id;
				VmWareData.platformData = row;
				if (!flag)  $scope.selectTab('',1);//对平台进行操作时不对相应tab数据做更新
			};
			//获取云平台详情，进行数据整合
			var getVendorDetail = function () {
				httpLoad.loadData({
					url: '/cloudVendor/detail',
					method: 'GET',
					data: {id: VmWareData.platformId},
					success: function (data) {
						if (data.success) {
							var obj = ['clusterCount','hostCount','dataStoreCount','instance','templateCount'];
							obj.forEach(function (item) {
								VmWareData.platformData[item] = data.data[item];
							});
						}
					}
				});
			};
			$scope.sync = function ($event,row) {
				$event.stopPropagation();
				$scope.selectCell(row);
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/sync.html',
					controller: 'msyncVmModalCtrl',
					resolve: {
						id: function () {
							return row.id;
						}
					}
				});
				modalInstance.result.then(function () {
					loadTabData();
				}, function () {
				});
			};
			//请求列表数据
			var loadTabData = function (page) {
				switch (VmWareData.type) {
					case 'datacenter':
						if(page) VmWareData.datacenter.clearSearch();
						VmWareData.datacenter.getList(page);
						break;
					case 'cluster':
						if(page) VmWareData.cluster.clearSearch();
						VmWareData.cluster.getList(page);
						break;
					case 'host':
						if(page) {
							VmWareData.host.clearSearch();
							VmWareData.host.getSearchData();
						};
						VmWareData.host.getList(page);
						break;
					case 'vm':
						if(page) VmWareData.vm.clearSearch();
						VmWareData.vm.getList(page);
						break;
					case 'template':
						if(page) {
							VmWareData.template.clearSearch();
						};
						VmWareData.template.getList(page);
						break;
					case 'snapshot':
						if(page) {
							VmWareData.snapshot.clearSearch();
							VmWareData.snapshot.getSearchData();
						};
						VmWareData.snapshot.getList(page);
						break;
					case 'datastore':
						if(page) VmWareData.datastore.clearSearch();
						VmWareData.datastore.getList(page);
						break;
					case 'network':
						if(page) VmWareData.network.clearSearch();
						VmWareData.network.getList(page);
						break;
				}
			};
			//选择tab
			$scope.selectTab = function (type) {
				if (VmWareData.platformId===null) return;
				VmWareData.type = type || VmWareData.type;
				loadTabData(1);
			};
			var socketObj = {
				datacenter:'vmware.datacenter',
				cluster:'vmware.cluster',
				host:'vmware.host',
				vm:'vmware.vm',
				template:'vmware.template',
				snapshot:'vmware.snapshot',
				datastore:'vmware.datastore',
				network:'vmware.network'
			}
			//websocket异步操作
			webSocket.onmessage({
				message:function (data) {
					if($rootScope.currentUrl=='app.config.vmwareTab'&&(data.operate==socketObj[VmWareData.type]||data.operate=='vmware.sync'&&data.data.vendorId==VmWareData.platformId)){
						loadTabData();
						if(data.operate=='vmware.sync' || VmWareData.platformData.isLoadDetail) getVendorDetail();
					}
				}
			});
		}
	]);
	//添加/编辑平台ctrl
	app.controller('addVmPlatformModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', 'id',
		function ($scope, $modalInstance, $stateParams, httpLoad, id) {
			$scope.modalName = '添加平台';
			var url = '/cloudVendor/create';
			$scope.typeData = ["5.5", "6.0"];
			$scope.updateData = {};
			$scope.updateData.CloudVendor = {
				version: '5.5',
				type: 'VMWARE'
			};
			var getData = function () {
				var list = ["id", "name", "version", "account", "password", "address","remark"];
				httpLoad.loadData({
					url: '/cloudVendor/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success) {
							var data = data.data;
							for (var i = 0; i < list.length; i++) {
								var name = list[i];
								$scope.updateData.CloudVendor[name] = data[name];
							}
						}
					}
				});
			};
			if (id) {
				$scope.modalName = '编辑平台';
				url = '/cloudVendor/modify';
				$scope.inputDisable = true;
				getData();
			}
			;
			//保存按钮
			$scope.ok = function () {
				httpLoad.loadData({
					url: url,
					method: 'POST',
					data: $scope.updateData,
					success: function (data) {
						if (data.success) {
							var text = '平台添加成功';
							if (id) text = '平台编辑成功';
							$scope.pop(text);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//同步ctrl
	app.controller('msyncVmModalCtrl', ['$rootScope', '$scope', '$modalInstance', 'httpLoad', 'id',
		function ($rootScope, $scope, $modalInstance, httpLoad, id) {
			$scope.ok = function () {
				httpLoad.loadData({
					url: '/cloudVendor/sync',
					method: 'POST',
					data: {
						id: id
					},
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close();
						}
					}
				});
			};
			$scope.cancel = function () {
				$modalInstance.dismiss('cancel'); // 退出
			};
		}]);
	//----------END--------------
	
	//--------数据中心-------
	app.controller('vmDatacenterCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, VmWareData) {
			$rootScope.link = '/statics/css/user.css';//引入页面样式
			$scope.param = {
				page:1,
				rows: 10
			};
			//获取数据中心列表
			$scope.getData = function (page) {
				$scope.showDetail = false;
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/vdc/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.vmDatacenterList = data.data.rows;
							$scope.totalCount = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							$scope.vmDatacenterList.forEach(function (item) {
								item.cpuUsage = {'width': (item.cpuUsed * 100 / item.cpuTotal) + '%'};
								item.cpuUsageTxt = item.cpuUsed + '/' + item.cpuTotal;
								item.memoryUsage = {'width': (item.memUsed * 100 / item.memTotal) + '%'};
								item.memoryUsageTxt = item.memUsed + '/' + item.memTotal;
								item.diskUsage = {'width': (item.diskUsed * 100 / item.diskTotal) + '%'};
								item.diskUsageTxt = item.diskUsed + '/' + item.diskTotal;
							});
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.search = function(page){
				//对参数进行处理，去除空参数
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				};
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.datacenter = {
				getList: $scope.search,
				getSearchData:function () {
					
				},
				clearSearch:function () {
					var obj = ['searchByName'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	//----------END--------------
	
	//-----------集群-------
	app.controller('vmClusterCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, VmWareData) {
			//$rootScope.moduleTitle = '资源中心 > 平台管理';//定义当前页
			$rootScope.link = '/statics/css/user.css';//引入页面样式
			$scope.param = {
				page:1,
				rows: 10
			};
			//获取集群列表
			$scope.getData = function (page) {
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/cluster/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmClusterList = data.data.rows;
							$scope.totalCount = VmWareData.platformData.clusterCount = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							$scope.vmClusterList.forEach(function (item) {
								item.cpuUsage = {'width': (item.cpuUsed * 100 / item.cpuTotal) + '%'};
								item.cpuUsageTxt = item.cpuUsed + '/' + item.cpuTotal;
								item.memoryUsage = {'width': (item.memUsed * 100 / item.memTotal) + '%'};
								item.memoryUsageTxt = item.memUsed + '/' + item.memTotal;
								item.diskUsage = {'width': (item.diskUsed * 100 / item.diskTotal) + '%'};
								item.diskUsageTxt = item.diskUsed + '/' + item.diskTotal;
							});
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			$scope.paramd = {
				page: 1,
				rows: 10
			};
			//详情主机记录
			$scope.getHostData = function (page, id) {
				$scope.paramd.page = page || $scope.paramd.page;
				$scope.paramd.params = JSON.stringify([{"param": {"clusterId": id || $scope.supplierDetail.id,"vendorId": VmWareData.platformId}, "sign": "EQ"}]);
				httpLoad.loadData({
					url: '/vhost/list',
					method: 'POST',
					data:$scope.paramd,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmHostList = data.data.rows;
							$scope.totalCountd = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageDatad = true;
							} else $scope.isImageDatad = false;
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			$scope.goDetail = function ($event, id) {
				$event.stopPropagation();
				httpLoad.loadData({
					url: '/cluster/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success && data.data) {
							$scope.supplierDetail = data.data;
							$scope.showDetail = $scope.isActive = true;
						}
					}
				});
				$scope.getHostData(1,id);
			};
			//新增集群
			$scope.add = function () {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/cluster/addModal.html',
					controller: 'addVmClusterModalCtrl',
					backdrop: 'static'
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			};
			//删除集群
			$scope.remove = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/template/delModal.html',
					controller: 'delModalCtrl',
					backdrop: 'static',
					resolve: {
						tip: function () {
							return '移除集群将同时移除所有主机和虚拟机，您确定要执行此操作吗？';
						},
						btnList: function () {
							return [{name: '是（Y）', type: 'btn-info'}, {name: '否（N）', type: 'btn-cancel'}];
						}
					}
				});
				modalInstance.result.then(function () {
					httpLoad.loadData({
						url: '/cluster/remove',
						method: 'POST',
						data: {id: id},
						success: function (data) {
							if (data.success) {
								$scope.getData();
								$scope.pop(data.message);
							}
						}
					});
				});
			};
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.operationHost = function (flag,itemData) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/cluster/hostModal.html',
					controller: 'operationVmClusterHostModalCtrl',
					backdrop: 'static',
					resolve:{
						itemData:function () {
							return itemData;
						},
						flag:function () {
							return flag;
						}
					}
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			};
			$scope.search = function(page){
				//对参数进行处理，去除空参数
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				};
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.cluster = {
				getList: $scope.search,
				getSearchData:function () {
				},
				clearSearch:function () {
					var obj = ['searchByName'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	app.controller('addVmClusterModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','VmWareData',
		function ($scope, $modalInstance, $stateParams, httpLoad, VmWareData) {
			//获取数据中心列表
			(function () {
				httpLoad.loadData({
					url: '/vdc/list',
					method: 'POST',
					data: {
						params:angular.toJson([{"param": {"vendorId": VmWareData.platformId}, "sign": "EQ"}]),
						simple:true
					},
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmDatacenterList = data.data.rows;
						}
					}
				});
			})();
			//保存按钮
			$scope.ok = function () {
				httpLoad.loadData({
					url: '/cluster/create',
					method: 'POST',
					data: {
						clusterName:$scope.name,
						vendorId:VmWareData.platformId,
						vdcName:$scope.vdcData.name,
						vdcId:$scope.vdcData.id
					},
					success: function (data) {
						if (data.success) {
							$scope.pop('集群创建成功');
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//移入移除主机
	app.controller('operationVmClusterHostModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','itemData','flag',
		function ($scope, $modalInstance, $stateParams, httpLoad, itemData, flag) {
			$scope.modalTitle = '移入主机';$scope.itemsByPage = 8;
			var params = {
				page:1,
				rows:10000
			};
			var url;
			if(flag==1){
				url = '/cluster/addHost';
				params.params = angular.toJson([{"param": {"vdcId":itemData.vdcId}, "sign": "EQ"}]);
			}else {
				$scope.modalTitle = '移出主机';
				url = '/cluster/removeHost';
				params.params = angular.toJson([{"param": {"clusterId":itemData.id}, "sign": "EQ"}]);
			};
			(function () {
				var formatData = function (data) {
					//移除移入数据处理
					if(flag == 1){
						$scope.listData = [];
						data.data.rows.forEach(function (item) {
							if(item.clusterId != itemData.id) {
								if(item.clusterId&&!item.maintain&&item.powerState =='RUNNING') item.disabled = true;
								$scope.listData.push(item);
							}
						});
					}else {
						$scope.listData = data.data.rows;
						$scope.listData.forEach(function (item) {
							item.disabled = (!item.maintain&&item.powerState =='RUNNING');
						});
					};
				}
				httpLoad.loadData({
					url: '/vhost/list',
					method: 'POST',
					data: params,
					noParam: true,
					success: function (data) {
						if (data.success) {
							formatData(data);
						}
					}
				});
			})();
			//保存按钮
			$scope.ok = function () {
				var hosts= [];
				$scope.listData.forEach(function (item) {
					if(item.select) hosts.push(item.id);
				});
				if(hosts.length == 0) return $scope.pop('请至少选择一台主机','error');
				var postData = {
					vendorId:itemData.vendorId,
					vdcId:itemData.vdcId,
					vdcName:itemData.vdcName,
					hosts:hosts,
					clusterId:itemData.id
				};
				if(flag == 1){
					postData.clusterName = itemData.name;
				}
				httpLoad.loadData({
					url: url,
					method: 'POST',
					data: postData,
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//----------END--------------
	
	//-------宿主机----------
	app.controller('vmHostCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, VmWareData) {
			$scope.param = {
				page:1,
				rows: 10
			};
			var disableOperate = function (data) {
				if(data.clusterId && data.powerState == 'RUNNING' && !data.maintain){
					data.disDel = true;
				};
				if(data.clusterId&&!data.maintain&&data.powerState =='RUNNING'){
					data.disMove = true;
				};
			};
			var getCluster = function(){
				httpLoad.loadData({
					url: '/cluster/list',
					method: 'POST',
					data:{
						simple:true,
						params:angular.toJson([{"param": {"vendorId": VmWareData.platformId}, "sign": "EQ"}])
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.clusterListData = data.data.rows;
						}
					}
				});
			};
			//获取集群列表
			$scope.getData = function (page) {
				$scope.showDetail = false;
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/vhost/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmHostList = data.data.rows;
							$scope.totalCount = VmWareData.platformData.hostCount = data.data.total;
							$scope.vmHostList.forEach(function (item) {
								disableOperate(item);
							});
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
							} else $scope.isImageData = false;
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			$scope.paramd = {
				page: 1,
				rows: 10
			};
			//详情虚机记录
			$scope.getVmData = function (page, id) {
				$scope.paramd.page = page || $scope.paramd.page;
				$scope.paramd.params = JSON.stringify([{"param": {"hostId": id || $scope.supplierDetail.id,"vendorId": VmWareData.platformId,"isTemplate": false}, "sign": "EQ"}]);
				httpLoad.loadData({
					url: '/vm/list',
					method: 'POST',
					data: $scope.paramd,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmList = data.data.rows;
							$scope.totalCountd = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageDatad = true;
								return;
							} else $scope.isImageDatad = false;
							angular.forEach($scope.vmList, function (data, index) {
								//状态
								data.isShowRecover = data.isRecover = data.isShowPaused = data.isPaused = data.isShowSuspended = data.isSuspended = data.isShowActive = data.isActive = data.isShowStart = data.isStart = data.isShowStop = data.isStop = false;
								switch (data.status) {
									case 'RUNNING':
										data.isShowStop = true;
										data.isStop = false;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = false;
										data.isShowSuspended = true;
										data.isSuspended = false;
										data.statusColor = 'success';
										break;
									case 'STOPPED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = false;
										data.isShowRecover = true;
										data.isRecover = false;
										data.isShowActive = true;
										data.isActive = false;
										data.statusColor = 'warning';
										break;
									case 'STOPPING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'STARTING':
										data.isShowStop = true;
										data.isStop = true;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'SUSPENDED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowActive = true;
										data.isActive = false;
										data.statusColor = 'primary';
										break;
									case 'SUSPENDING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'ACTIVING':
										data.isShowStop = true;
										data.isStop = true;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'PAUSED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowRecover = true;
										data.isRecover = false;
										data.statusColor = 'warning';
										break;
									case 'PAUSING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowRecover = true;
										data.isRecover = true;
										data.statusColor = 'default';
										break;
									case 'RECOVERING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'PAUSING':
										data.isShowStop = true;
										data.isStop = true;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.statusColor = 'default';
										break;
									case 'BUILDING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'RESTARTING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'EXPIRED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'EXCEPTION':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'danger';
										break;
									case 'SYNSEXCEPTION':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'danger';
										break;
								}
							});
						} else {
							$scope.isImageDatad = true;
						}
					}
				});
			};
			$scope.goDetail = function ($event, id) {
				$event.stopPropagation();
				httpLoad.loadData({
					url: '/vhost/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success && data.data) {
							$scope.supplierDetail = data.data;
							$scope.showDetail = $scope.isActive = true;
						}
					}
				});
				$scope.getVmData(1,id);
			};
			$scope.createNetwork = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/network/addNetworkHostModal.html',
					controller: 'addVmNetworkHostModalCtrl',
					backdrop: 'static',
					resolve: {
						id: function () {
							return id;
						}
					}
				});
				modalInstance.result.then(function (data) {
				});
			};
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.catPerformance = function (name) {
				var obj = {
					flag:1,
					name:name,
					vendorId:VmWareData.platformId
				};
				sessionStorage.setItem('performance',JSON.stringify(obj))
				$state.go('app.config.vmperformance')
			};
			//添加
			$scope.add= function () {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/host/addHostModal.html',
					controller: 'addVmHostModalCtrl',
					backdrop: 'static',
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			};
			//删除
			$scope.remove = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/template/delModal.html',
					controller: 'delModalCtrl',
					backdrop: 'static',
					resolve: {
						tip: function () {
							return '确定要删除该宿主机吗？';
						},
						btnList: function () {
							return [{name: '删除', type: 'btn-info'}, {name: '取消', type: 'btn-cancel'}];
						}
					}
				});
				modalInstance.result.then(function () {
					httpLoad.loadData({
						url: '/vhost/remove',
						method: 'POST',
						data: {id: id},
						success: function (data) {
							if (data.success) {
								$scope.getData();
								$scope.pop(data.message);
							}
						}
					});
				});
			};
			//移动
			$scope.move = function (row) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/host/moveHostModal.html',
					controller: 'moveVmHostModalCtrl',
					backdrop: 'static',
					resolve: {
						itemData: function () {
							return row;
						}
					}
				});
				modalInstance.result.then(function () {
					$scope.getData();
				});
			};
			$scope.operation = function (flag,id) {
				var templateUrl = '/statics/tpl/template/delModal.html';
				var url,popText;
				switch(flag/1){
					case 1:
						url = '/vhost/reconnect';
						popText = '您确定要连接所选主机吗？';
						break;
					case 2:
						url = '/vhost/disconnect';
						popText = '您确定要断开与所选主机的连接吗？';
						break;
					case 3:
						url = '/vhost/entermaintain';
						templateUrl = '/statics/tpl/config/vmware/host/entermaintainModal.html';
						popText = '处于维护模式下的主机不会执行任何与虚拟机相关的功能，包括虚拟机置备操作。若要进入维护模式，所有虚拟机必须关机或移至其他主机。可能需要人工干预。' +
							'是否要将选定的主机置于维护模式吗？';
						break;
					case 4:
						url = '/vhost/exitmaintain';
						popText = '确定要退出维护模式吗？';
						break;
				};
				var modalInstance = $modal.open({
					templateUrl: templateUrl,
					controller: 'delModalCtrl',
					backdrop: 'static',
					resolve: {
						tip: function () {
							return popText;
						},
						btnList: function () {
							return [{name: '确定', type: 'btn-info'}, {name: '取消', type: 'btn-cancel'}];
						}
					}
				});
				modalInstance.result.then(function () {
					httpLoad.loadData({
						url: url,
						method: 'POST',
						data: {id: id},
						success: function (data) {
							if (data.success) {
								$scope.getData();
								$scope.pop(data.message);
							}
						}
					});
				});
			};
			$scope.search = function(page){
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
					
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId,
					clusterId:$scope.searchByCluster
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.host = {
				getList: $scope.search,
				getSearchData:function () {
					getCluster();
				},
				clearSearch:function () {
					var obj = ['searchByName','searchByCluster'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	//新增主机
	app.controller('addVmHostModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','VmWareData',
		function ($scope, $modalInstance, $stateParams, httpLoad, VmWareData) {
			(function () {
				httpLoad.loadData({
					url: '/vdc/list',
					method: 'POST',
					data:{
						simple:true,
						params:angular.toJson([{"param": {"vendorId": VmWareData.platformId}, "sign": "EQ"}])
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.vdcListData = data.data.rows;
						}
					}
				});
			})();
			$scope.loadClusterData = function () {
				var param = [{"param": {"vendorId": VmWareData.platformId,vdcId:$scope.vdcData.id}, "sign": "EQ"}]
				httpLoad.loadData({
					url: '/cluster/list',
					method: 'POST',
					data: {
						simple:true,
						params:angular.toJson(param)
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.clusterListData = data.data.rows;
						}
					}
				});
			}
			$scope.addData = {
				vendorId:VmWareData.platformId,
			};
			//保存按钮
			$scope.ok = function () {
				$scope.addData.vdcId = $scope.vdcData.id;
				$scope.addData.vdcName = $scope.vdcData.name;
				if($scope.clusterData){
					$scope.addData.clusterId = $scope.clusterData.id;
					$scope.addData.clusterName = $scope.clusterData.name;
				}
				httpLoad.loadData({
					url: '/vhost/create',
					method: 'POST',
					data: $scope.addData,
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//移动主机
	app.controller('moveVmHostModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','itemData',
		function ($scope, $modalInstance, $stateParams, httpLoad, itemData) {
			$scope.itemData = itemData;
			(function () {
				var param = [{"param": {"vendorId": itemData.vendorId,vdcId:itemData.vdcId}, "sign": "EQ"}]
				httpLoad.loadData({
					url: '/cluster/list',
					method: 'POST',
					data: {
						simple:true,
						params:angular.toJson(param)
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.clusterListData = data.data.rows;
							$scope.clusterListData.forEach(function (item,key) {
								if(item.id==itemData.clusterId) {
									$scope.clusterListData.splice(key,1);
									return;
								}
							});
						}
					}
				});
			})();
			$scope.addData = {
				vendorId:itemData.vendorId,
				vdcName:itemData.vdcName,
				hostId:itemData.id,
				hostName:itemData.name
			};
			//保存按钮
			$scope.ok = function () {
				if($scope.clusterData){
					$scope.addData.clusterId = $scope.clusterData.id;
					$scope.addData.clusterName = $scope.clusterData.name;
				}
				httpLoad.loadData({
					url: '/vhost/move',
					method: 'POST',
					data: $scope.addData,
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	app.controller('addVmNetworkHostModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','VmWareData','id',
		function ($scope, $modalInstance, $stateParams, httpLoad, VmWareData, id) {
			$scope.addData = {
				vendorId:VmWareData.platformId,
				portGroupNum:120
			};
			$scope.validatePort = function () {
				if($scope.addData.portGroupNum > 1024) {
					$scope.addData.portGroupNum = 120;
				}
			};
			//获取详情
			(function () {
				httpLoad.loadData({
					url: '/vhost/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success) {
							$scope.networkCards = [];
							$scope.addData.vdcId = data.data.vdcId;
							data.data.netcards.forEach(function (item) {
								if(!item.used) $scope.networkCards.push(item);
							});
						}
					}
				});
			})();
			//处理网卡数据
			var formatData = function () {
				var obj = {},	arr = [];
				$scope.networkCards.forEach(function (item) {
					if(item.select){
						arr.push(item.id);
					}
				});
				obj[id] = arr;
				return obj;
			}
			//保存按钮
			$scope.ok = function () {
				$scope.addData.hosts = formatData();
				httpLoad.loadData({
					url: '/vhost/createVSwitch',
					method: 'POST',
					data: $scope.addData,
					success: function (data) {
						if (data.success) {
							$scope.pop('网络创建成功');
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//----------END--------------
	
	//-------虚机----------
	app.controller('vmListCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, VmWareData) {
			$scope.param = {
				page:1,
				rows: 10
			};
			var judgeOperate = function (data) {
					switch (data.status) {
						case 'STOPPING':
						case 'STARTING':
						case 'SUSPENDING':
						case 'RESTARTING':
						case 'EXCEPTION':
						case 'UNKNOWNON':
						case 'ACTIVING':
						case 'SYNSEXCEPTION':
						case 'BUILDING':
							data.disEdit = true;
							data.disDel = true;
							data.disPer = true;
							data.disStart = true;
							data.disStop = true;
							data.disRestart = true;
							data.disSuspended = true;
							data.disSnap = true;
							data.disClone = true;
							data.disMig = true;
							data.statusColor = 'default';
							break;
						case 'RUNNING':
							data.disDel = true;
							data.disStart = true;
							data.statusColor = 'success';
							break;
						case 'STOPPED':
							data.disPer = true;
							data.disStop = true;
							data.disRestart = true;
							data.disSuspended = true;
							data.statusColor = 'warning';
							break;
						case 'SUSPENDED':
							data.disEdit = true;
							data.disRestart = true;
							data.disSuspended = true;
							data.statusColor = 'primary';
							break;
						case 'UNKNOWNOFF':
						case 'UNKNOWNSUSPEND':
							data.disEdit = true;
							data.disDel = true;
							data.disStart = true;
							data.disStop = true;
							data.disRestart = true;
							data.disSuspended = true;
							data.disPer = true;
							data.disSnap = true;
							data.disClone = true;
							data.statusColor = 'default';
							break;
					}
			};
			//获取云主机列表
			$scope.getData = function (page) {
				$scope.showDetail = false;
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/vm/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmList = data.data.rows;
							$scope.totalCount = VmWareData.platformData.instance = data.data.total;
							$scope.vmList.forEach(function (item) {
								judgeOperate(item);
							});
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			//状态数据
			$scope.statusData = [{"value": "RUNNING", "name": "运行中"}, {
				"value": "STOPPED",
				"name": "关机"
			}, {"value": "SUSPENDED", "name": "挂起"}, {"value": "EXCEPTION", "name": "异常"},{"value": "UNKNOWN", "name": "断开"}];
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.paramd = {
				page: 1,
				rows: 10
			};
			//详情操作记录
			$scope.getOperateData = function (page, id) {
				$scope.paramd.page = page || $scope.paramd.page;
				$scope.paramd.params = JSON.stringify([{"param": {"vmId": id || $scope.vmDetail.id}, "sign": "EQ"}]);
				httpLoad.loadData({
					url: '/res/event/list',
					method: 'POST',
					data: $scope.paramd,
					noParam: true,
					success: function (data) {
						if (data.success) {
							if (data.data) {
								$scope.isImageDatad = false;
								$scope.eventList = data.data.rows;
								$scope.totalCountd = data.data.total;
								if(!$scope.totalCountd) $scope.isImageDatad = true;
							}
						} else {
							$scope.isImageDatad = true;
						}
					}
				});
			};
			$scope.detail = {
				snapParam:{
					page:1,
					rows:10
				},
				getSnapshot:function (page,id) {
					$scope.detail.snapParam.page = page || $scope.detail.snapParam.page;
					$scope.detail.snapParam.params = JSON.stringify([{"param": {"vmId": id || $scope.vmDetail.id}, "sign": "EQ"}]);
					httpLoad.loadData({
						url: '/snapshot/list',
						method: 'POST',
						data: $scope.detail.snapParam,
						noParam: true,
						success: function (data) {
							if (data.success) {
								$scope.detail.snapshotList = data.data.rows;
								$scope.detail.totalSnap = data.data.total;
								if (data.data.rows.length == 0) {
									$scope.detail.noneSnap = true;
									return;
								} else $scope.detail.noneSnap = false;
								angular.forEach($scope.detail.snapshotList, function (data, index) {
									if (data.status == 'AVAILABLE') data.status1 = '正常';
									else if (data.status == 'INAVAILABLE') data.status1 = '不正常';
								});
							} else {
								$scope.detail.noneSnap = true;
							}
						}
					});
				}
			};
			//跳转详情页
			$scope.goDetail = function ($event, id) {
				$event.stopPropagation();
				httpLoad.loadData({
					url: '/vm/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success && data.data) {
							$scope.vmDetail = data.data;
							$scope.showDetail = $scope.isActive = true;
							//状态处理
							switch ($scope.vmDetail.status) {
								case 'RUNNING':
									$scope.vmDetail.statusColor = "success";
									break;
								case 'STOPPING':
								case 'STARTING':
								case 'SUSPENDING':
								case 'ACTIVING':
								case 'RECOVERING':
								case 'PAUSING':
								case 'RESTARTING':
								case 'BUILDING':
									$scope.vmDetail.statusColor = "default";
									break;
								case 'SUSPENDED':
									$scope.vmDetail.statusColor = "primary";
									break;
								case 'PAUSED':
								case 'STOPPED':
									$scope.vmDetail.statusColor = "warning";
									break;
								case 'EXCEPTION':
								case 'SYNSEXCEPTION':
									$scope.vmDetail.statusColor = "danger";
									break;
							}
							
							//密码处理
							var password = '';
							if ($scope.vmDetail.password) {
								for (var i = 0; i < $scope.vmDetail.password.length; i++) {
									password += '*';
								}
								$scope.vmDetail.password = password;
							}
						}
					}
				});
				//获取列表数据
				$scope.getOperateData(1, id);
				$scope.detail.getSnapshot(1,id);
			};
			//新增
			$scope.add = function () {
				$state.go('app.config.vmwareAdd', {id: VmWareData.platformId});
			};
			//创建快照
			$scope.createSnapshot = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/vm/addSnapshotModal.html',
					controller: 'addSnapshotModalCtrl',
					backdrop: 'static',
					resolve: {
						id: function () {
							return id;
						}
					}
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			};
			//克隆
			$scope.clone = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/vm/cloneHostModal.html',
					controller: 'cloneHostModalCtrl',
					backdrop: 'static',
					resolve: {
						id: function () {
							return id;
						}
					}
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			};
			//热迁移
			$scope.migrate = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/vm/migrateModal.html',
					controller: 'migrateModalCtrl',
					backdrop: 'static',
					resolve: {
						id: function () {
							return id;
						}
					}
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			}
			//编辑
			$scope.update = function (id) {
				sessionStorage.setItem('vmTabvendor', JSON.stringify({id: VmWareData.platformId}));
				$state.go('app.config.vmwareUpdate', {id: id});
			};
			$scope.showPassword = function (value) {
				$scope[value] = !$scope[value];
			};
			//删除6,备份3，快照4，恢复0，暂停7，挂起8，激活9，重启1，开机2，关机5
			$scope.remove = function ($event, row, value, isAction) {
				if ($event) $event.stopPropagation();
				//重启vmware tools判断
				if(value==1&&!row.toolsInstalled) return $scope.pop('该虚拟机未安装VMware Tools，无法重启','info')
				if (isAction == true) return;
				var txt = '';
				if (value == 6) {
					if (row.status != 'STOPPED') {
						txt = '虚机正在运行中，无法删除';
					}
				}
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/vm/remove.html',
					controller: 'removevmModalCtrl',
					resolve: {
						removeData: function () {
							return {id: row.id};
						},
						value: function () {
							return value;
						},
						txt: function () {
							return txt;
						}
					}
				});
				modalInstance.result.then(function () {
					$scope.getData();
				}, function () {
				});
			};
			$scope.catPerformance = function (name) {
				var obj = {
					flag:2,
					name:name,
					vendorId:VmWareData.platformId
				};
				sessionStorage.setItem('performance',JSON.stringify(obj))
				$state.go('app.config.vmperformance')
			};
			$scope.search = function(page){
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName,
					managerIp:$scope.searchByIP
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId,
					isTemplate: false
				});
				//虚机特殊需求
				if($scope.searchByStatus){
					if($scope.searchByStatus.indexOf('UNKNOWN') > -1) param1.status = 'UNKNOWN';
					else param2.status = $scope.searchByStatus;
				}
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.vm = {
				getList: $scope.search,
				getSearchData:function () {
				
				},
				clearSearch:function () {
					var obj = ['searchByName','searchByStatus','searchByIP'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	//删除ctrl，以及虚机操作
	app.controller('removevmModalCtrl', ['$scope', '$modalInstance', 'httpLoad', 'removeData', 'value', 'txt',
		function ($scope, $modalInstance, httpLoad, removeData, value, txt) {
			var url;
			//删除6,备份3，快照4，恢复0，停止7，挂起8，激活9，重启1，开机2，关机5,克隆10
			if (value == 0) {
				$scope.actionName = '恢复';
				url = '/vm/recovery';
			}
			if (value == 1) {
				$scope.actionName = '重启';
				url = '/vm/restart';
			}
			if (value == 3) {
				$scope.actionName = '备份';
				url = '/vm/backup';
			}
			if (value == 4) {
				$scope.actionName = '快照';
				url = '/vm/snapshoot';
			}
			if (value == 6) {
				$scope.actionName = '删除';
				url = '/vm/remove';
			}
			if (value == 7) {
				$scope.actionName = '停止';
				url = '/vm/pause';
			}
			if (value == 8) {
				$scope.actionName = '挂起';
				url = '/vm/suspend';
			}
			if (value == 9) {
				$scope.actionName = '激活';
				url = '/vm/active';
			}
			if (value == 2) {
				$scope.actionName = '开机';
				url = '/vm/start';
			}
			if (value == 5) {
				$scope.actionName = '关机';
				url = '/vm/stop';
			}
			if (value == 10) {
				$scope.actionName = '克隆';
				url = '/vm/clone';
			}
			if (value == 11) {
				$scope.actionName = '热迁移';
				url = '/vm/clone';
			}
			$scope.ok = function () {
				httpLoad.loadData({
					url: url,
					method: 'POST',
					data: removeData,
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message)
							$modalInstance.close();
						}
					}
				});
			};
			$scope.cancel = function () {
				$modalInstance.dismiss('cancel'); // 退出
			}
		}]);
	//虚机创建快照
	app.controller('addSnapshotModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', 'VmWareData', 'id',
		function ($scope, $modalInstance, $stateParams, httpLoad, VmWareData, id) {
			$scope.ok = function () {
				httpLoad.loadData({
					url: '/vm/snapshot',
					data: {
						id: VmWareData.platformId,
						serverId: id,
						name: $scope.name,
						remark: $scope.remark
					},
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//虚机克隆
	app.controller('cloneHostModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', '$modal', 'VmWareData', 'id',
		function ($scope, $modalInstance, $stateParams, httpLoad, $modal, VmWareData, id) {
		$scope.addData = {
			id:id
		};
			//获取数据存储数据
			var getDatastore = function(vhostId){
				var params = {
						simple : true
					},
					searchParam = [{"param":{"vendorId":$stateParams.id,"vhostId":vhostId,category:'PM'},"sign":"EQ"}];
				params.params = JSON.stringify(searchParam);
				httpLoad.loadData({
					url:'/datastore/list',
					method: 'POST',
					data:params,
					//data: {"id":$stateParams.id,"vdcId":vdcId,"clusterId":clusterId,"vhostId":vhostId},
					noParam: true,
					success:function(data){
						if(data.success){
							$scope.datastoreData = data.data.rows;
						}
					}
				});
			};
			//获取目标主机数据
			var getVhost = function(id,from){
				var searchParam = [];
				if(from=='vdc'){
					searchParam = [{"param":{"vendorId":$stateParams.id,"vdcId":id},"sign":"EQ"}]
				}else if(from=='cluster'){
					searchParam = [{"param":{"vendorId":$stateParams.id,"clusterId":id},"sign":"EQ"}]
				}
				var params = {
					page:1,
					rows:10000
				};
				params.params = JSON.stringify(searchParam);
				httpLoad.loadData({
					url:'/vhost/list',
					method: 'POST',
					data:params,
					//data: {"id":$stateParams.id,"vdcId":vdcId,"clusterId":clusterId},
					noParam: true,
					success:function(data){
						if(data.success){
							$scope.vhostData = data.data.rows;
						}
					}
				});
			};
			//获取集群数据
			var getCluster = function(vdcId){
				var params = {
						simple : true
					},
					searchParam = [{"param":{"vendorId":$stateParams.id,"vdcId":vdcId},"sign":"EQ"}];
				params.params = JSON.stringify(searchParam);
				httpLoad.loadData({
					url:'/cluster/list',
					method: 'POST',
					data:params,
					//data: {"id":$stateParams.id,"vdcId":vdcId},
					noParam: true,
					success:function(data){
						if(data.success){
							$scope.clusterData = data.data.rows;
						}
					}
				});
			};
			//获取数据中心数据
			var getVdc = function(){
				var params = {
						simple : true
					},
					searchParam = [{"param":{"vendorId":VmWareData.platformId},"sign":"EQ"}];
				params.params = JSON.stringify(searchParam);
				httpLoad.loadData({
					url:'/vdc/list',
					method: 'POST',
					data:params,
					noParam: true,
					success:function(data){
						if(data.success){
							$scope.vdcData = data.data.rows;
						}
					}
				});
			};
			getVdc();
			//选择数据中心
			$scope.chooseVdc = function(item){
				$scope.vdcData.forEach(function(data){
					data.isVdcActive = false;
					if(data.id==item.id){
						data.isVdcActive = !item.isVdcActive;
					}
					if(data.isVdcActive){
						$scope.vdcId = data.id;
						$scope.clusterId = '';$scope.addData.hostname = '';$scope.addData.datastore = '';$scope.datastoreData = '';
						getCluster($scope.vdcId);
						getVhost($scope.vdcId,'vdc');
						return;
					}
				});
			};
			//选择集群
			$scope.chooseCluster = function(item){
				$scope.clusterData.forEach(function(data){
					data.isClusterActive = false;
					if(data.id==item.id){
						data.isClusterActive = !item.isClusterActive;
					}
					if(data.isClusterActive){
						$scope.clusterId = data.id;
						$scope.addData.hostname = '';$scope.addData.datastore = '';$scope.datastoreData = '';
						getVhost($scope.clusterId,'cluster');
						return;
					}
				});
			};
			//选择目标主机
			$scope.chooseVhost = function(item){
				if(!item.connected || item.maintain) return $scope.pop('无法选择该虚拟机，因为该虚拟机未连接、处于维护模式下，或者在更改其维护状态','info')
				$scope.vhostData.forEach(function(data){
					data.isVhostActive = false;
					if(data.id==item.id){
						data.isVhostActive = !item.isVhostActive;
					}
					if(data.isVhostActive){
						$scope.addData.hostname = data.name;
						$scope.addData.hostId = data.id;
						$scope.addData.datastore = '';$scope.datastoreData = '';
						getDatastore(data.id);
						return;
					}
				});
			};
			//选择数据存储
			$scope.chooseDatastore = function(item){
				$scope.datastoreData.forEach(function(data){
					data.isDatastoreActive = false;
					if(data.id==item.id){
						data.isDatastoreActive = !item.isDatastoreActive;
					}
					if(data.isDatastoreActive){
						$scope.addData.datastore = data.name;
						$scope.addData.storeId = data.id;
						return;
					}
				});
			};
			$scope.operationStatus = {
				REMOVE:'删除中',
				ADD:'新增中',
				EDIT:'编辑中'
			};
			$scope.getNetworkData = function () {
				httpLoad.loadData({
					url:'/vm/detail',
					method: 'GET',
					data:{
						id: id
					},
					success:function(data){
						if(data.success){
							$scope.selectNetworkListData = data.data.netcards;
							$scope.selectNetworkListData.forEach(function (item) {
								item.operation = 'NORMAL';
								item.cacheOperation = 'NORMAL';
								item.disLabel = item.label.replace(/Network adapter/g,'网络适配器');
							})
						}
					}
				});
			};
			$scope.getNetworkData();
			$scope.recoveryNetwork = function (row) {
				row.operation = row.cacheOperation;
			};
			$scope.delNetwork = function (row,index) {
				if(!row.cacheOperation){
					$scope.selectNetworkListData.splice(index,1);
				}else{
					row.cacheOperation = row.operation;
					row.operation = 'REMOVE';
				}
			};
			$scope.operateNetwork = function (row,index) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/vm/addNetworkModal.html',
					controller: 'operateNetworkModalCtrl',
					backdrop: 'static',
					resolve: {
						id: function () {
							return $scope.addData.hostId;
						},
						itemData:function () {
							return row
						}
					}
				});
				modalInstance.result.then(function (data) {
					if(row){
						$scope.selectNetworkListData[index] = data;
					}else{
						$scope.selectNetworkListData.push(data);
					}
				});
			};
			var formatNetworks = function () {
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var arr = [];
				$scope.selectNetworkListData.forEach(function (item) {
					if(item.operation != 'NORMAL'){
						var obj = toObjFormat({
							name:item.name,
							type:item.type,
							label:item.label,
							operation:item.operation
						});
						arr.push(obj);
					}
				});
				return arr;
			}
			$scope.ok = function () {
				if(!$scope.addData.hostname&&$scope.addData.hostname==''){
					$scope.pop('请选择目标主机','error');
					return;
				}
				if(!$scope.addData.datastore&&$scope.addData.datastore==''){
					$scope.pop('请选择数据存储','error');
					return;
				}
				$scope.addData.operations = formatNetworks();
				httpLoad.loadData({
					url: '/vm/clone',
					data: $scope.addData,
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//虚机热迁移
	app.controller('migrateModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', 'VmWareData', 'id',
		function ($scope, $modalInstance, $stateParams, httpLoad, VmWareData, id) {
			//获取物理机列表
			(function () {
				httpLoad.loadData({
					url: '/vhost/list',
					method: 'POST',
					data: {
						simple: true,
						params: JSON.stringify([{"param": {"vendorId": VmWareData.platformId}, "sign": "EQ"}])
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.vmHostList = data.data.rows;
						}
					}
				});
			})();
			$scope.ok = function () {
				var hostData = JSON.parse($scope.hostData)
				httpLoad.loadData({
					url: '/vm/migrate',
					data: {
						id: id,
						hostname: hostData.name,
						hostId: hostData.id
					},
					success: function (data) {
						if (data.success) {
							$scope.pop(data.message);
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//----------END--------------
	
	//--------模板------
	app.controller('vmTemplateCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', '$stateParams', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, $stateParams, VmWareData) {
			$scope.param = {
				page:1,
				rows: 10
			};
			//获取云主机列表
			$scope.getData = function (page) {
				$scope.showDetail = false;
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/vm/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmList = data.data.rows;
							$scope.totalCount = VmWareData.platformData.templateCount = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							angular.forEach($scope.vmList, function (data, index) {
								//状态
								data.isShowRecover = data.isRecover = data.isShowPaused = data.isPaused = data.isShowSuspended = data.isSuspended = data.isShowActive = data.isActive = data.isShowStart = data.isStart = data.isShowStop = data.isStop = false;
								switch (data.status) {
									case 'RUNNING':
										data.isShowStop = true;
										data.isStop = false;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = false;
										data.isShowSuspended = true;
										data.isSuspended = false;
										data.statusColor = 'success';
										break;
									case 'STOPPED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = false;
										data.isShowRecover = true;
										data.isRecover = false;
										data.isShowActive = true;
										data.isActive = false;
										data.statusColor = 'warning';
										break;
									case 'STOPPING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'STARTING':
										data.isShowStop = true;
										data.isStop = true;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'SUSPENDED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowActive = true;
										data.isActive = false;
										data.statusColor = 'primary';
										break;
									case 'SUSPENDING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'ACTIVING':
										data.isShowStop = true;
										data.isStop = true;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'PAUSED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowRecover = true;
										data.isRecover = false;
										data.statusColor = 'warning';
										break;
									case 'PAUSING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowRecover = true;
										data.isRecover = true;
										data.statusColor = 'default';
										break;
									case 'RECOVERING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'PAUSING':
										data.isShowStop = true;
										data.isStop = true;
										data.isShowRestart = true;
										data.isRestart = false;
										data.isShowStart = false;
										data.statusColor = 'default';
										break;
									case 'BUILDING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
										break;
									case 'RESTARTING':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = false;
										data.isShowPaused = true;
										data.isPaused = true;
										data.isShowSuspended = true;
										data.isSuspended = true;
										data.statusColor = 'default';
										break;
									case 'EXPIRED':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'default';
									case 'EXCEPTION':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'danger';
										break;
									case 'SYNSEXCEPTION':
										data.isShowStop = false;
										data.isShowRestart = true;
										data.isRestart = true;
										data.isShowStart = true;
										data.isStart = true;
										data.isShowRecover = true;
										data.isRecover = true;
										data.isShowActive = true;
										data.isActive = true;
										data.statusColor = 'danger';
										break;
								}
							});
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			//状态数据
			//新增
			$scope.add = function ($event, row) {
				$event.stopPropagation();
				$state.go('app.config.vmwareTemplateAdd', {id: VmWareData.platformId, templateId: row.id + '#' + row.name});
			};
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.paramd = {
				page: 1,
				rows: 10
			};
			//获取详情操作记录
			$scope.getOperateData = function (page, id) {
				$scope.paramd.page = page || $scope.paramd.page;
				$scope.paramd.params = JSON.stringify([{"param": {"vmId": id || $scope.vmDetail.id}, "sign": "EQ"}]);
				httpLoad.loadData({
					url: '/res/event/list',
					method: 'POST',
					data: $scope.paramd,
					noParam: true,
					success: function (data) {
						if (data.success) {
							if (data.data) {
								$scope.eventList = data.data.rows;
								$scope.totalCountd = data.data.total;
								$scope.isImageDatad = false;
							}
						} else {
							$scope.isImageDatad = true;
						}
					}
				});
			};
			//跳转详情页
			$scope.detail = function ($event, id) {
				$event.stopPropagation();
				httpLoad.loadData({
					url: '/vm/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success && data.data) {
							$scope.vmDetail = data.data;
							$scope.showDetail = $scope.isActive = true;
							//状态处理
							switch ($scope.vmDetail.status) {
								case 'RUNNING':
									$scope.vmDetail.statusColor = "success";
									break;
								case 'STOPPED':
								case 'PAUSED':
									$scope.vmDetail.statusColor = "warning";
									break;
								case 'SUSPENDED':
									$scope.vmDetail.statusColor = "primary";
									break;
								case 'STOPPING':
								case 'STARTING':
								case 'SUSPENDING':
								case 'ACTIVING':
								case 'PAUSING':
								case 'RECOVERING':
								case 'PAUSING':
								case 'BUILDING':
								case 'RESTARTING':
									$scope.vmDetail.statusColor = "default";
									break;
								case 'EXCEPTION':
									$scope.vmDetail.statusColor = "danger";
									break;
							}
							;
							//密码处理
							var password = '';
							if ($scope.vmDetail.password) {
								for (var i = 0; i < $scope.vmDetail.password.length; i++) {
									password += '*';
								}
								$scope.vmDetail.password = password;
							}
						}
					}
				});
				$scope.getOperateData(1, id);
			};
			$scope.search = function(page){
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
					
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId,
					isTemplate: true
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.template = {
				getList: $scope.search,
				getSearchData:function () {
				},
				clearSearch:function () {
					var obj = ['searchByName'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	//----------END--------------
	
	//--------快照--------
	app.controller('vmSnapshotCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, VmWareData) {
			$scope.param = {
				page:1,
				rows: 10
			};
			//获取快照管理列表
			$scope.getData = function (page) {
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/snapshot/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.snapshotList = data.data.rows;
							$scope.totalCount = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							angular.forEach($scope.snapshotList, function (data, index) {
								if (data.status == 'AVAILABLE') data.status1 = '正常';
								else if (data.status == 'INAVAILABLE') data.status1 = '不正常';
							});
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			//删除
			$scope.remove = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/template/delModal.html',
					controller: 'delModalCtrl',
					backdrop: 'static',
					resolve: {
						tip: function () {
							return '你确定要删除该快照吗？';
						},
						btnList: function () {
							return [{name: '删除', type: 'btn-danger'}, {name: '取消', type: 'btn-default'}];
						}
					}
				});
				modalInstance.result.then(function () {
					httpLoad.loadData({
						url: '/snapshot/remove',
						method: 'POST',
						data: {id: id},
						success: function (data) {
							if (data.success) {
								$scope.pop(data.message);
								$scope.getData(1);
							}
						}
					});
				});
			};
			//快照恢复
			$scope.recovery = function (id) {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/template/delModal.html',
					controller: 'delModalCtrl',
					backdrop: 'static',
					resolve: {
						tip: function () {
							return '你确定要恢复该快照吗？';
						},
						btnList: function () {
							return [{name: '恢复', type: 'btn-primary'}, {name: '取消', type: 'btn-default'}];
						}
					}
				});
				modalInstance.result.then(function () {
					httpLoad.loadData({
						url: '/snapshot/revert',
						method: 'POST',
						data: {id: id},
						success: function (data) {
							if (data.success) {
								$scope.pop(data.message);
								$scope.getData(1);
							}
						}
					});
				});
			}
			$scope.search = function(page){
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
					
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId,
					vmId:$scope.searchByVm
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			var getVm = function(){
				httpLoad.loadData({
					url: '/vm/list',
					method: 'POST',
					data:{
						simple:true,
						params:angular.toJson([{"param": {"vendorId": VmWareData.platformId,isTemplate: false,}, "sign": "EQ"}])
					},
					noParam: true,
					success: function (data) {
						if (data.success) {
							$scope.vmListData = data.data.rows;
						}
					}
				});
			};
			VmWareData.snapshot = {
				getList: $scope.search,
				getSearchData:function () {
					getVm();
				},
				clearSearch:function () {
					var obj = ['searchByName','searchByVm'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	//----------END--------------
	
	//---------存储---------
	app.controller('vmDatastoreCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, VmWareData) {
			$scope.param = {
				page:1,
				rows: 10
			};
			//获取集群列表
			$scope.getData = function (page) {
				$scope.showDetail = false;
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/datastore/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmDatastoreList = data.data.rows;
							$scope.totalCount = VmWareData.platformData.dataStoreCount = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							$scope.vmDatastoreList.forEach(function (item) {
								item.capacityUsed = item.capacity - item.freeSpace;
								item.capacityUsage = {'width': (item.capacityUsed * 100 / item.capacity) + '%'};
								item.capacityUsageTxt = item.capacityUsed + '/' + item.capacity;
							});
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			var getDetailVm = function (id) {
				httpLoad.loadData({
					url: '/datastore/servers',
					method: 'GET',
					data: {id:id},
					success: function (data) {
						if (data.success) {
							$scope.vmListData = data.data;
							if ($scope.vmListData.length == 0) {
								$scope.isImageDataVm = true;
							} else $scope.isImageDataVm = false;
						} else {
							$scope.isImageDataVm = true;
						}
					}
				});
			};
			var getDetailHost = function (id) {
				httpLoad.loadData({
					url: '/datastore/hosts',
					method: 'GET',
					data: {id:id},
					success: function (data) {
						if (data.success) {
							$scope.hostListData = data.data;
							if ($scope.hostListData.length == 0) {
								$scope.isImageDataHost = true;
							} else $scope.isImageDataHost = false;
						} else {
							$scope.isImageDataHost = true;
						}
					}
				});
			};
			$scope.goDetail = function ($event, id) {
				$event.stopPropagation();
				httpLoad.loadData({
					url: '/datastore/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success && data.data) {
							$scope.supplierDetail = data.data;
							$scope.showDetail = $scope.isActive = true;
						}
					}
				});
				getDetailVm(id);
				getDetailHost(id);
			};
			$scope.itemsByPage = 10;
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.search = function(page){
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId,
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.datastore = {
				getList: $scope.search,
				getSearchData:function () {
				},
				clearSearch:function () {
					var obj = ['searchByName'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	//----------END--------------
	
	//-------网络---------
	app.controller('networkCtrl', ['$scope', 'httpLoad', '$rootScope', '$modal', '$state', '$timeout', '$stateParams', 'VmWareData',
		function ($scope, httpLoad, $rootScope, $modal, $state, $timeout, $stateParams, VmWareData) {
			$scope.param = {
				page:1,
				rows: 10
			};
			//获取网络管理列表
			$scope.getData = function (page) {
				$scope.param.page = page || $scope.param.page;
				httpLoad.loadData({
					url: '/network/list',
					method: 'POST',
					data: $scope.param,
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.networkList = data.data.rows;
							$scope.totalCount = data.data.total;
							if (data.data.rows.length == 0) {
								$scope.isImageData = true;
								return;
							} else $scope.isImageData = false;
							angular.forEach($scope.networkList, function (data, index) {
								if (data.status == 'RUNNING') data.status1 = '运行中';
								else data.status1 = '异常';
								if (data.shared == true) data.shared1 = '是';
								if (data.shared == false) data.shared1 = '否';
							});
						} else {
							$scope.isImageData = true;
						}
					}
				});
			};
			$scope.add = function () {
				var modalInstance = $modal.open({
					templateUrl: '/statics/tpl/config/vmware/network/addNetworkModal.html',
					controller: 'addVmNetworkModalCtrl',
					backdrop: 'static'
				});
				modalInstance.result.then(function (data) {
					$scope.getData();
				});
			};
			var getDetailVm = function (id) {
				httpLoad.loadData({
					url: '/network/servers',
					method: 'GET',
					data: {id:id},
					success: function (data) {
						if (data.success) {
							$scope.vmListData = data.data;
							if ($scope.vmListData.length == 0) {
								$scope.isImageDataVm = true;
							} else $scope.isImageDataVm = false;
						} else {
							$scope.isImageDataVm = true;
						}
					}
				});
			}
			//跳转详情页
			$scope.goDetail = function ($event, id) {
				$event.stopPropagation();
				httpLoad.loadData({
					url: '/network/detail',
					method: 'GET',
					data: {id: id},
					success: function (data) {
						if (data.success && data.data) {
							$scope.supplierDetail = data.data;
							$scope.showDetail = $scope.isActive = true;
						}
					}
				});
				getDetailVm(id);
			};
			$scope.itemsByPage = 10;
			//返回
			$scope.goBack = function () {
				$scope.isActive = false;
				$timeout(function () {
					$scope.showDetail = false;
				}, 200);
			};
			$scope.search = function(page){
				var toObjFormat = function(obj) {
					for (var a in obj) {
						if (obj[a] === "") delete obj[a];
					}
					return obj;
				}
				var params = [];
				var param1 = toObjFormat({
					name: $scope.searchByName
					
				});
				var param2 = toObjFormat({
					vendorId: VmWareData.platformId,
				});
				if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
				if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
				var param = {
					rows: 10,
					params: angular.toJson(params)
				};
				angular.extend($scope.param,param);
				$scope.getData(page)
			};
			VmWareData.network = {
				getList: $scope.search,
				getSearchData:function () {
				},
				clearSearch:function () {
					var obj = ['searchByName'];
					angular.forEach(obj,function(data){
						$scope[data] = '';
					});
				}
			};
		}
	]);
	app.controller('addVmNetworkModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad','VmWareData',
		function ($scope, $modalInstance, $stateParams, httpLoad, VmWareData) {
		$scope.addData = {
			vendorId:VmWareData.platformId,
			portGroupNum:4
		};
			//获取数据中心列表
			(function () {
				httpLoad.loadData({
					url: '/vdc/list',
					method: 'POST',
					data: {
						params:angular.toJson([{"param": {"vendorId": VmWareData.platformId}, "sign": "EQ"}]),
						simple:true
					},
					noParam: true,
					success: function (data) {
						if (data.success && data.data.rows) {
							$scope.vmDatacenterList = data.data.rows;
						}
					}
				});
			})();
			$scope.getNetwork = function () {
				if(!$scope.vdcData) return;
				$scope.addData.vdcName = $scope.vdcData.name;
				$scope.addData.vdcId = $scope.vdcData.id;
				httpLoad.loadData({
					url: '/vdc/hosts',
					method:'GET',
					data: {
						id:$scope.vdcData.id
					},
					success: function (data) {
						if (data.success) {
							$scope.networkCards = [];
							$scope.networkCards = data.data;
						}
					}
				});
			};
			//控制显示隐藏
			$scope.showFun = function (item) {
				item.show = !item.show;
			};
			//全选
			$scope.selectAll = function (item) {
				item.netcards.forEach(function (row) {
					row.select = item.select;
					item.show = true;
				});
			}
				//选一个
				$scope.selectCell = function (item) {
					var index = 0;
					item.netcards.forEach(function (row) {
					if(row.select) return item.select = true;
						else index++;
					});
					if(index == (item.netcards.length)) item.select = false;
				};
				//处理网卡数据
			var formatData = function () {
				var obj = {};
				$scope.networkCards.forEach(function (item) {
					if(item.select&&item.netcards.length>0){
						var arr = [];
						item.netcards.forEach(function (itm) {
						if(itm.select)	arr.push(itm.id);
						});
						obj[item.id] = arr;
					}
				});
				return obj;
			}
			//保存按钮
			$scope.ok = function () {
				$scope.addData.hosts = formatData();
				if(angular.toJson($scope.addData.hosts).length==2) return $scope.pop('请至少选择一组网卡','error');
				httpLoad.loadData({
					url: '/network/createDSwitch',
					method: 'POST',
					data: $scope.addData,
					success: function (data) {
						if (data.success) {
							$scope.pop('网络创建成功');
							$modalInstance.close(data);
						}
					}
				});
			}
			$scope.cancle = function () {
				$modalInstance.dismiss('cancel');
			};
		}
	]);
	//----------END--------------
})();